// JavaScript Document
function createDiv(id,c,p,w,h,top,left,color,alpha){
	var newDiv = document.createElement("div");
	newDiv.id=id;
	newDiv.className=c;
	newDiv.style.position = p;
	newDiv.style.zIndex = "9000";
	newDiv.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=2,opacity="+alpha*100+",finishOpacity="+alpha*100+")";
	newDiv.style.opacity=alpha;
	newDivWidth = w;
	newDivHeight = h;
	newDiv.style.width = newDivWidth;
	newDiv.style.height = newDivHeight;
	
	newDiv.style.top = top+"px";
	newDiv.style.left = left+"px";
	newDiv.style.background = color;
	return newDiv;
};
function showIe6Tip(id)
{
	var bgdiv = createDiv("bgdiv","bgdiv","absolute",document.documentElement.clientWidth+"px",document.documentElement.clientHeight+"px",0,0,"#000000",0.75);
	document.body.appendChild(bgdiv);
	var tip = document.getElementById(id);
	tip.style.zIndex="9999";
	$t = $(tip);
	$t.show();
	var idTop = (document.body.clientHeight-$t.height())*0.5+"px";
	var idelft = (document.documentElement.clientWidth-$t.width())*0.5+"px";
	tip.style.top=idTop;
	tip.style.left=idelft;
	$t.find("a").click(function(){
		$(this).parent().hide();
		document.body.removeChild(bgdiv);
		return false;
	});
};
function showTip(id)
{
	var tip = document.getElementById(id);
	tip.style.zIndex="9999";
	$t = $(tip);
	$t.show();
	var idTop = (document.body.clientHeight-$t.height())*0.5+"px";
	var idelft = (document.documentElement.clientWidth-$t.width())*0.5+"px";
	tip.style.top=idTop;
	tip.style.left=idelft;
	$t.find("a").click(function(){
		$(this).parent().hide();
	});
};
function checkFormObj1(field,alerttxt){
	if (field.value==null||field.value=="")
    {
		alert(alerttxt);
		return false;
	}
  	else{
		return true;
	}
}

function checkFormObj2(field,len,alerttxt){
	if (field.value.length>len)
    {
		alert(alerttxt);
		return false;
	}
  	else {
		return true;
	}
}

var yywind = yywind||{};
//首页-导航
yywind.menu = function(){
	var index = $(".menu li.on").index();
	$(".menu li").bind("mouseover",function(){
		$(this).addClass("on").siblings().removeClass("on");
	});
	$(".menu ul").bind("mouseout",function(){
		$(this).find("li:eq("+index+")").addClass("on").siblings().removeClass("on");
	});
};
//首页-banner
yywind.banner = function(){
	var index = 0;
	var len = $(".banner li").length;
	$(".banner li:eq("+index+")").show();
	var timer = setInterval(onTimer,4000);
	function onTimer()
	{
		$(".banner li:eq("+index+")").fadeOut("slow");
		index+=1;
		if(index==len)
		{
			index=0;
		}
		$(".banner li:eq("+index+")").fadeIn("slow");
		$(".bannerBar a:eq("+index+")").addClass("on").siblings().removeClass("on");
	}
	$(".bannerBar a").hover(function(){
		clearInterval(timer);
	},function(){
		timer = setInterval(onTimer,4000);
	});
	$(".bannerBar a").bind("click",function(){
		$(".banner li:eq("+index+")").fadeOut("slow");
		index = $(this).index();
		$(".banner li:eq("+index+")").fadeIn("slow");
		$(".bannerBar a:eq("+index+")").addClass("on").siblings().removeClass("on");
	});
};
//关于我们-九宫格
yywind.grid9 = function(){
	var p = $(".grid9");
	var _w = p.width();
	var _cos = _w/91;
	var dirt = "r";
	$(".grid9 li.m").hover(function(){
		$(this).addClass("on").siblings().removeClass("on");
		var pop = $(this).find(".pop");
		var _x = $(this).index()%_cos;
		if((_x*91+272)>_w)
		{
			dirt="l";
		} 
		else
		{
			dirt="r";
		}
		switch(dirt)
		{
			case 'r':
				pop.animate({"left":91,opacity:"show"}); 
				break;
			case 'l':
				pop.css({left:-91});
				pop.animate({"left":-182,opacity:"show"}); 
				break;
			default:
				break;
		}
	},function(){
		var pop = $(this).find(".pop");
		switch(dirt)
		{
			case 'r':
				pop.animate({"left":0,opacity:"hide"},100); 
				break;
			case 'l':
				pop.animate({"left":-91,opacity:"hide"},100); 
				break;
			default:
				break;
		}
	});
};
//案例展示
yywind.caseShow = function(){
	$(".caseType").slideDown("slow");
};
//案例展示-返回顶部
yywind.goTop = function(){
    var btn;
    btn = $(".caseDetail").find(".top");
	var btn = $(".caseDetail .top");
	var _x = (document.documentElement.scrollWidth-1002)*0.5-34;
	btn.css({"right":_x});
	btn.click(function(){
		$(document.documentElement).animate({scrollTop:0});
	});
	$(window).resize(function(){
		_x = (document.documentElement.scrollWidth-1002)*0.5-34;
		btn.css({"right":_x});
	});
};
//联系我们
yywind.ajaxForm=function(id){
	var o=$(id);
	var url=o.attr("href");
	o.click(function(){
		var data={};
		switch(id)
		{
			case "#msgBtn":
				var username=$("#guestbookForm input[name=username]").val();
				var contact=$("#guestbookForm input[name=contact]").val(); 
				var content=$("#guestbookForm textarea[name=content]").val(); 
				data={username:username,contact:contact,content:content};
				if(checkGuestBookForm($("#guestbookForm")[0]))
				{
					$.ajax({
						type:'POST',
						data:data,
						dataType:'json',
						url:url,
						error:function (XMLHttpRequest, textStatus, errorThrown) {
						    alert("发表留言失败！");
						},
						success:function(msg){
							if(msg.no==0)
							{
								alert(msg.msg);
							}
							else
							{
								var form=$("#guestbookForm");
								form.find("input[name=username]").val("");
								form.find("input[name=contact]").val("");
								form.find("textarea[name=content]").val("");
								showTip("tipok");
							}
						}
					})
				}
				break;
			default:
				break;
		}
		return false;
	});
};
function checkGuestBookForm(form){
	if(!checkFormObj1(form.username,"姓名不能为空!"))
	{
		form.username.focus();	
		return false;
	}	
	else if(!checkFormObj2(form.username,20,"姓名不能超过20个字符!"))
	{
		form.username.focus();	
		return false;
	}
	else if(!checkFormObj1(form.contact,"联系方式不能为空!"))
	{
		form.contact.focus();	
		return false;
	}	
	else if(!checkFormObj2(form.contact,50,"联系方式不能超过50个字符!"))
	{
		form.contact.focus();	
		return false;
	}
	else
	{
		return true;
	}
}

$(function(){
	yywind.menu();
	yywind.banner();
	yywind.grid9();
	yywind.caseShow();
	yywind.goTop();
	yywind.ajaxForm("#msgBtn");
});